package it.aekidna.cleverplatypus.workflow.template
{
	import it.aekidna.cleverplatypus.mediator.MediationRegistry;
	import it.aekidna.cleverplatypus.workflow.AbstractWorkflowTask;

	public class ListenCommandEventTask extends AbstractWorkflowTask
	{
		public var eventName : String;
		public var params : Array;
		public var handler : Function;
		public var count : int = 1;
		
		private var _countDown : int;
		
		override public function begin() : void
		{
			_countDown = count;
			MediationRegistry.getInstance().registerMediator( eventName, params, onEvent, this );
		}

		private function onEvent( ... inParams ) : void
		{
			if( handler != null )
			{
				handler.apply( this, inParams );
			}
			_countDown--;
			if( _countDown == 0 )
			{
				complete();
			}
		}		
	}
}